<template>
   <div class="app-container">


     <div class="app-container__section" >
       <PlusSearch
         v-bind="getSearchBinds()"
         :columns="columns"
         v-model="searchForm"
       >
       </PlusSearch>
     </div>
     <div class="app-container__section">
       <PlusTable
         v-bind="getBinds()"
         :columns="columns"
       >
         <template #title>
           <table-title />
         </template>
         <template #toolbar>
           <el-button
             type="primary"
             plain
             icon="Plus"
             @click="handleAdd"
             v-hasPermi="['system:post:add']"
           >新增</el-button>
           <el-button
             type="success"
             plain
             icon="Edit"
             :disabled="gSelection.length !== 1"
             @click="handleUpdate"
             v-hasPermi="['system:post:edit']"
           >修改</el-button>
           <el-button
             type="danger"
             plain
             icon="Delete"
             :disabled="gSelection.length === 0"
             @click="handleDelete"
             v-hasPermi="['system:post:remove']"
           >删除</el-button>
           <el-button
             type="warning"
             plain
             icon="Download"
             @click="handleExport"
             v-hasPermi="['system:post:export']"
           >导出</el-button>
           <ClockTimer :task="gGetList" />
           <SearchAreaControl v-model="gShowSearchForm" />
         </template>

         <template #plus-cell-status="scope">
           <dict-tag  :options="sys_normal_disable" :value="scope.row.status"></dict-tag>
         </template>
         <template #plus-cell-action="scope">
             <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:post:edit']">修改</el-button>
             <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:post:remove']">删除</el-button>
         </template>
       </PlusTable>
     </div>

      <!-- 添加或修改岗位对话框 -->
      <el-dialog :title="title" v-model="open" width="500px" append-to-body>
         <el-form ref="postRef" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="岗位名称" prop="postName">
               <el-input v-model="form.postName" placeholder="请输入岗位名称" />
            </el-form-item>
            <el-form-item label="岗位编码" prop="postCode">
               <el-input v-model="form.postCode" placeholder="请输入编码名称" />
            </el-form-item>
            <el-form-item label="岗位顺序" prop="postSort">
               <el-input-number v-model="form.postSort" controls-position="right" :min="0" />
            </el-form-item>
            <el-form-item label="岗位状态" prop="status">
               <el-radio-group v-model="form.status">
                  <el-radio
                     v-for="dict in sys_normal_disable"
                     :key="dict.value"
                     :label="dict.value"
                  >{{ dict.label }}</el-radio>
               </el-radio-group>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
               <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
         </el-form>
         <template #footer>
            <div class="dialog-footer">
               <el-button type="primary" @click="submitForm">确 定</el-button>
               <el-button @click="cancel">取 消</el-button>
            </div>
         </template>
      </el-dialog>
   </div>
</template>

<script setup name="Post">
import { listPost, addPost, delPost, getPost, updatePost } from "@/api/system/post";
import { PlusSearch, PlusTable } from 'plus-pro-components'
import { useTablePage } from '@/hooks/useTablePage.js'
import { useActions } from '@/hooks/useActions.js'

const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");

// =============================================
const columns = computed(()=>[
  {
    label: '岗位编号',
    prop: 'postId',
    hideInSearch: true,
    tableColumnProps: {
      align: 'center'
    },
  },
  {
    label: '岗位编码',
    prop: 'postCode',
    tableColumnProps: {
      align: 'center'
    },
  },
  {
    label: '岗位名称',
    prop: 'postName',
    tableColumnProps: {
      align: 'center'
    },
  },
  {
    label: '岗位排序',
    prop: 'postSort',
    hideInSearch: true,
    tableColumnProps: {
      align: 'center'
    },
  },
  {
    label: '状态',
    prop: 'status',
    tableColumnProps: {
      align: 'center'
    },
    valueType: 'select',
    options: sys_normal_disable.value,
  },
  {
    label: '创建时间',
    prop: 'createTime',
    hideInSearch: true,
    tableColumnProps: {
      align: 'center'
    },
    valueType:'date-picker',
    fieldProps: {
      type: 'daterange',
      valueFormat: "YYYY-MM-DD"
    },
  },
  {
    label: '操作',
    prop: 'action',
    width: '160px',
    hideInSearch: true,
    tableColumnProps: {
      align: 'center',
      fixed:'right'
    }
  },
])

// 搜索条件
const searchForm = ref({})

const {
  gRouteMeta,
  gTableData,
  gTotal,
  gLoadingStatus,
  gPageInfo,
  gReset,
  gGetList,
  gOnPageChange,
  gSelection,
  gOnSelectionChange,
  gShowSearchForm,
  gResetForm,
  gResetPage,
  getBinds,
  getSearchBinds
} = useTablePage(listPost, {
  searchForm,
})


const { deleteAction } = useActions();
function handleDelete(row) {
  deleteAction({
    api: delPost,
    uniqueKey:'postId',
    selection: row.postId || gSelection.value
  }).then(() => {
    gGetList();
  });
}

// =============================================

const open = ref(false);
const title = ref("");

const data = reactive({
  form: {},
  rules: {
    postName: [{ required: true, message: "岗位名称不能为空", trigger: "blur" }],
    postCode: [{ required: true, message: "岗位编码不能为空", trigger: "blur" }],
    postSort: [{ required: true, message: "岗位顺序不能为空", trigger: "blur" }],
  }
});

const {  form, rules } = toRefs(data);

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    postId: undefined,
    postCode: undefined,
    postName: undefined,
    postSort: 0,
    status: "0",
    remark: undefined
  };
  proxy.resetForm("postRef");
}


/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加岗位";
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const postId = row.postId || gSelection.value[0].postId;
  getPost(postId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改岗位";
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["postRef"].validate(valid => {
    if (valid) {
      if (form.value.postId != undefined) {
        updatePost(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          gGetList();
        });
      } else {
        addPost(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          gGetList();
        });
      }
    }
  });
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download("system/post/export", {
    ...searchForm.value
  }, `post_${new Date().getTime()}.xlsx`);
}

</script>
